import pygame
import math
import sys


# 函数
def pao(pos_x, pos_y, vel_x, vel_y):
    vel_y += g * t
    pos_x += vel_x * t / k
    pos_y += vel_y * t / k
    return pos_x, pos_y, vel_y


# 定义颜色
black = 0, 0, 0
white = 255, 255, 255

k = 0.3  # 距离缩小参数
g = 9.8  # 重力常数
t = 8e-3 # 时间放大参数

def ini(x):
    theta = x*math.pi/180
    v0 = 50
    vel_x = v0*math.cos(theta)  # 水平初速度
    vel_y = -v0*math.sin(theta)
    return vel_x, vel_y

pygame.init()  # 初始化
screen = pygame.display.set_mode((1000, 600))  # 创建窗口
font = pygame.font.Font(r'C:\Windows\Fonts\simkai.ttf', 30)  # 显示中文需要字体，否则可略过
text = font.render("斜抛", 1, white)
guiji = font.render(".", 1, white)
pygame.display.set_caption("斜抛模拟")

pos_x1, pos_y1 = 10, 500
ls_pos1 = [(pos_x1, pos_y1)]
vel_x1, vel_y1 = ini(45)
flag1 = 0

pos_x2, pos_y2 = 10, 500
ls_pos2 = [(pos_x2, pos_y2)]
vel_x2, vel_y2 = ini(47)
flag2 = 0
while 1:  # 主循环
    for event in pygame.event.get():
        if event.type in (pygame.QUIT, pygame.KEYDOWN):
            sys.exit()  # 按键响应，按键后退出

    screen.fill(black)  # 刷新背景
    # 轨迹
    for p in ls_pos1:
        screen.blit(guiji, p)
    for p in ls_pos2:
        screen.blit(guiji, p)

    pos_x1, pos_y1, vel_y1 = pao(pos_x1, pos_y1, vel_x1, vel_y1)
    if pos_y1 > 500:
        flag1 = 1
    else:
        pos1 = (pos_x1, pos_y1)
        ls_pos1.append(pos1)

    pos_x2, pos_y2, vel_y2 = pao(pos_x2, pos_y2, vel_x2, vel_y2)
    if pos_y2 > 500:
        flag2 = 1
    else:
        pos2 = (pos_x2, pos_y2)
        ls_pos2.append(pos2)
    if flag1*flag2:
        continue
    v = '竖直速度= %.1f m/s' % (-vel_y1)  # 速度大小
    speed = font.render(v, 1, white)
    screen.blit(speed, (100, 50))
    pygame.display.update()  # 刷新

